package com.efesco.stat.operation;

import com.bstek.dorado.data.*;
import com.bstek.dorado.data.db.SqlDataset;
import com.bstek.dorado.common.*;
import com.efesco.common.Tools;
import com.efesco.system.User;

/**
 * changeService_changeEmployeeDatasetListener
 */
public class changeService_changeEmployeeDatasetListener extends AbstractDatasetListener {
  public boolean beforeLoadData(Dataset dataset)
     throws Exception {
	    String sql="",foreign_enterprise_id="",condition="";
	  	
	  	DoradoContext context = DoradoContext.getContext();
	    Object obj = context.getAttribute(context.SESSION, "User");
	    if (obj != null) {
	    	User user = (User) obj;
    		foreign_enterprise_id=user.getForeignEnterpriseId();
	    	String startDate = Tools.format(dataset.parameters().getDate("startDate"),"yyyy-MM-dd");
	    	String endDate = Tools.format(dataset.parameters().getDate("endDate"),"yyyy-MM-dd");
    		if (!startDate.equals(""))  {
    			condition = condition + " and il.create_date >= to_date('"+startDate+" 00:00:00','yyyy-mm-dd hh24:mi:ss')";
    		}
    		if (!endDate.equals(""))  {
    			condition = condition + " and il.create_date <= to_date('"+endDate+" 23:59:59','yyyy-mm-dd hh24:mi:ss')";
    		}
    		
	    	if (dataset.parameters().getString("type").equals("0"))  {
	    		sql="select cmp.company_code,cmp.company_chinese_name,emp.employee_code,emp.employee_name,eoop.on_post_date " +
	    				"from s_in_char_service_transaction st ,s_in_char_list il," +
	    				"s_employee_on_off_post eoop,s_employee emp,s_single_company cmp,s_in_char_service cs " +
	    				"where  st.in_char_id=il.in_char_id and il.employee_on_off_id=eoop.employee_on_off_id " +
	    				"and eoop.employee_id=emp.employee_id and eoop.single_company_id=cmp.single_company_id " +
	    				"and st.in_char_service_id = cs.in_char_service_id and cs.service_type=30 and st.status<>'0' " +
	    				"and il.status=0 and eoop.employee_on_off_id in (select eoop.employee_on_off_id " +
	    				"from s_in_char_service_transaction st ,s_in_char_list il,s_employee_on_off_post eoop," +
	    				"s_in_char_service cs " +
	    				"where  st.in_char_id=il.in_char_id and il.employee_on_off_id=eoop.employee_on_off_id " +
	    				"and st.in_char_service_id = cs.in_char_service_id and cs.service_type=1 and st.status<>'0' " +
	    				"and il.status<>0 "+condition+") " +
	    				"and cmp.foreign_enterprise_id="+foreign_enterprise_id;
	    	}
	    	if (dataset.parameters().getString("type").equals("1"))  {
	    		sql="select cmp.company_code,cmp.company_chinese_name,emp.employee_code,emp.employee_name,eoop.on_post_date " +
				   		"from s_in_char_service_transaction st ,s_in_char_list il," +
				   		"s_employee_on_off_post eoop,s_employee emp,s_single_company cmp,s_in_char_service cs " +
				   		"where  st.in_char_id=il.in_char_id and il.employee_on_off_id=eoop.employee_on_off_id " +
				   		"and eoop.employee_id=emp.employee_id and eoop.single_company_id=cmp.single_company_id " +
				   		"and st.in_char_service_id = cs.in_char_service_id and cs.service_type=1 and st.status<>'0' " +
				   		"and il.status=0 and eoop.employee_on_off_id in (select eoop.employee_on_off_id " +
				   		"from s_in_char_service_transaction st ,s_in_char_list il,s_employee_on_off_post eoop," +
				   		"s_in_char_service cs " +
				   		"where  st.in_char_id=il.in_char_id and il.employee_on_off_id=eoop.employee_on_off_id " +
				   		"and st.in_char_service_id = cs.in_char_service_id and cs.service_type=30 and st.status<>'0' " +
				   		"and il.status<>0 "+condition+") " +
				   		"and cmp.foreign_enterprise_id="+foreign_enterprise_id;
	    	}

	    	
	    	if (!sql.equals(""))  {
	    		sql = sql + " order by cmp.company_code,emp.employee_code ";
	    	}
	    	SqlDataset sqlDataset=(SqlDataset)dataset;
	    	sqlDataset.setSql(sql);
	    }
    return true;
  }

}
